package de.wbh.gwt.form.layouter.java.server.servlet.rpc;

import java.io.File;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.google.common.collect.Lists;
import com.google.gwt.rpc.server.RpcServlet;

import de.wbh.gwt.form.layouter.gwt.rpc.PcfService;
import de.wbh.gwt.form.layouter.java.server.ConfigurationHelper;

public class PcfServiceImpl extends RpcServlet implements PcfService {

	private static String dir = ConfigurationHelper.getInstance().getHomeDirectory();

	@Override
	public List<PositionedField> getPositionedFields(String pcfName) {
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		List<PositionedField> fields = Lists.newArrayList();
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File(dir + "\\pcf\\" + pcfName + ".pcf"));

			NodeList nodes = doc.getElementsByTagName("print");
			for (int i = 0; i < nodes.getLength(); i++) {
				Element e = (Element) nodes.item(i);
				PositionedField field = new PositionedField();
				field.id = e.getAttribute("printText");
				field.id = field.id.substring(3, field.id.length() - 3);
				field.x = Integer.parseInt(e.getAttribute("printPosX"));
				field.y = Integer.parseInt(e.getAttribute("printPosY"));
				field.width = Integer.parseInt(e.getAttribute("printWidth"));
				field.height = Integer.parseInt(e.getAttribute("printHeight"));
				fields.add(field);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return fields;
	}

	@Override
	public void updatePcf(String pcfName, List<PositionedField> positionedFields) {
		/*
		 * <FileInformation>
		 * 
		 * <DokumentName>$$$Dokument-ID auf Server$$$</DokumentName>
		 * 
		 * </FileInformation>
		 * 
		 * <printConfig>
		 * 
		 * <printPage pageNr="1">
		 * 
		 * <print printText="$$$BeispielName$$$" printPosY="743" printPosX="438" printHeight="13" printWidth="120" pageNr="1">
		 * 
		 * <print/>
		 * 
		 * </printPage>
		 * 
		 * </printConfig>
		 */

		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		try {
			docBuilder = docFactory.newDocumentBuilder();

			// root elements
			Document doc = docBuilder.newDocument();
			Element pc = doc.createElement("printConfig");
			doc.appendChild(pc);
			Element pp = doc.createElement("printPage");
			pp.setAttribute("pageNr", "1");
			pc.appendChild(pp);
			for (PositionedField pf : positionedFields) {
				Element p = doc.createElement("print");
				p.setAttribute("printText", "$$$" + pf.id + "$$$");
				p.setAttribute("printPosY", "" + pf.y);
				p.setAttribute("printPosX", "" + pf.x);
				p.setAttribute("printHeight", "" + pf.height);
				p.setAttribute("printWidth", "" + pf.width);
				pp.appendChild(p);
			}

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			File path = new File(dir + "\\pcf");
			if (!path.exists()) {
				path.mkdir();
			}
			StreamResult result = new StreamResult(new File(path, pcfName + ".pcf"));

			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
